# 作者：一只牛吗
# 2025 年03月 07日 23时 08分 37秒
# 1278599690@qq.com
import copy

def use_list_copy():
    a = [1, 2, 3, 4, 5]
    b = a
    b[0] = 100
    print(a)  # [100, 2, 3, 4, 5]
    print(b)  # [100, 2, 3, 4, 5]

def use_copy():
    '''
    浅拷贝，使用列表自身的copy()方法，与copy模块无关
    :return:
    '''
    a = [1, 2, 3]
    b = a.copy()
    b[0] = 10
    print(a)  # [1, 2, 3]
    print(b)  # [10, 2, 3]


def ShallowCopy_test():
    '''
    浅拷贝测试可以见得，浅拷贝只进行了一层拷贝，只是无脑的将指针复制了，所以改变d的数据时，依然会影响c的数据
    :return:
    '''
    a = [1, 2, 3]
    b = [4, 5, 6]
    c = [a, b]
    d = c.copy()
    d[0][0] = 100
    print(c)  # [[100, 2, 3], [4, 5, 6]]
    print(d)  # [[100, 2, 3], [4, 5, 6]]
    print(id(c[0]), id(d[0]))
    print(id(c[1]), id(d[1]))

def use_deepcopy():
    '''
    深拷贝，使用copy模块的deepcopy()方法，需要调用copy模块
    :return:
    '''
    a = [1, 2, [3, 4]]
    b = copy.deepcopy(a)
    b[2][0] = 100
    print(a)  # [1, 2, [3, 4]]
    print(b)  # [1, 2, [100, 4]]


class Hero:
    def __init__(self, name, hp):
        self.name = name
        self.hp = hp
        self.outfit = ['衣服1', '衣服2', '衣服3']


def use_copy_module():
    old_hero = Hero('老王', 90)
    new_hero = copy.copy(old_hero)
    new_hero.name = '新王'
    new_hero.outfit.append('药水')
    print(old_hero.name, new_hero.name)
    print(old_hero.outfit, new_hero.outfit)

if __name__ == '__main__':
    # use_list_copy()
    # use_copy()
    # use_deepcopy()
    # ShallowCopy_test()
    use_copy_module()